package com.mixed;

import com.mixed.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@MultipartConfig
public class UploadPublicKey extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
            String savePath = getServletContext().getRealPath("/WEB-INF/publicKeyDir");
            String username = request.getParameter("userid");
            String tmpPath = savePath + "/" + username;
            File Tmpdir = new File(tmpPath);
            if(!Tmpdir.exists())
                Tmpdir.mkdirs();
            Part keypart = request.getPart("PublicKey");
            String tmpkeypath = tmpPath + "/" + "public_key.dat";
            keypart.write(tmpkeypath);
            try{
                String publickey = RSA.loadKeyByFile(tmpkeypath);
                Mysql.writeToMYSQL(username, publickey);
            }catch(Exception e) {
                e.printStackTrace();
            }
            File k = new File(tmpkeypath);
            k.delete();
            File f = new File(tmpPath);
            f.delete();
        }
}